package com.huayue.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.entity.CheckOut;
import com.huayue.entity.HousekeepOrder;
import com.huayue.entity.HousekeepOrderDetails;
import com.huayue.entity.User;
import com.huayue.service.HousekeepOrderService;
import com.huayue.service.RoomCheckOutService;
import com.huayue.util.OrderNumberHelper;

/**
 * 退房业务
 */
@Controller
@RequestMapping("/checkOut")
public class CheckOutController {

    // 自动注入
    @Autowired
    RoomCheckOutService svc;

    // 自动注入房态订单Service
    @Autowired
    HousekeepOrderService hkOService;

    /**
     * 根据房间id查询该房间的消费
     * 
     * @param id
     *            房间id
     * @return 退房单信息
     */
    @RequestMapping("/toCheckOut")
    public ModelAndView toCheckOut(Integer id) {
        // 创建一个用于格式化json数据的对象，方便向前台发送json数据
        FastJsonJsonView view = new FastJsonJsonView();
        // 根据房间id查询入住订单
        view.addStaticAttribute("code", 0);
        view.addStaticAttribute("base", svc.findRealOrderByRoomId(id));
        // 返回数据
        return new ModelAndView(view);
    }

    /**
     * 退房
     * 
     * @param checkOut
     * @param request
     * @return
     */
    @RequestMapping("/checkOut")
    public ModelAndView checkOut(CheckOut checkOut, HttpServletRequest request) {
        // 获取操作人信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        // 创建一个用于格式化json数据的对象，方便向前台发送json数据
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        // 进行退房操作
        boolean status = svc.checkOut(checkOut, user);

        // 添加房务订单
        HousekeepOrder order = new HousekeepOrder();
        // 获取一个房务订单号
        String orderNumber = OrderNumberHelper.getOrderNumber(OrderNumberHelper.FWORDER, "FW");
        // 将订单号放入订单信息对象中
        order.setOrdernumber(orderNumber);
        // 移除缓存的订单号
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.FWORDER, orderNumber);
        // 设置操作人
        order.setEnployee_id_apply(user.getId());
        // 设置订单时间
        order.setWorkdate_begin(new Date());
        // 添加订单详情
        List<HousekeepOrderDetails> list = new ArrayList<HousekeepOrderDetails>();
        HousekeepOrderDetails detail = new HousekeepOrderDetails();
        detail.setRoom_id(checkOut.getRoomId());
        detail.setHousekeep_id(5);
        detail.setRemark("退房");
        list.add(detail);
        // 将数据交由Service进行下房务单
        hkOService.addDetails(order, list);
        // 判断退房是否完成，并向填入信息
        if (status) {
            view.addStaticAttribute("icon", 1);
            view.addStaticAttribute("msg", "退房成功");
        } else {
            view.addStaticAttribute("icon", 2);
            view.addStaticAttribute("msg", "退房失败");
        }
        // 返回数据
        return new ModelAndView(view);
    }
}
